<?php

namespace App\Http\Controllers\BaseData;

use App\Http\Controllers\Controller;
use App\Models\Menu;
use App\Models\RolePermission;
use App\Models\Role;
use App\Models\RoleBasedAccessControl;
use App\Models\RoleMenu;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{

    public function index()
    {
        if (post("?")) {
            return (Role::search(post("search"))->getGrid());
        } else {
            $param = F();
            return view('BaseData.RoleController.index', $param);
        }
    }
    public function add()
    {
        if (post('?')) {
            $data = post('data');

            try {
                begin();
                $gdata = Role::create(array_filter($data));
                $gdata->saveitems(json_decode(post('items'), true));
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }
            return success("保存成功！");
        } else {
            setdata(['roles' => Role::toSelect()]);
            return view('BaseData.RoleController.add',   F());
        }
    }

    public function edit()
    {
        $id = get("id");
        $gdata = Role::where("id", $id)->first();

        if (post('?')) {
            $data = post('data');
            try {
                begin();
                $gdata->update(unsetNull($data));
                $gdata->saveitems(json_decode(post('items'), true));
                commit();
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }
            return success("保存成功！");
        } else {
            $param = F('add');
            $param['gdata'] = $gdata;
            $param['items'] = $gdata->rolesList()->relatedJoin()->get();
            setdata(['roles' => Role::where("id", "<>", $id)->toSelect()]);
            return view('BaseData.RoleController.add',  $param);
        }
    }

    public function delete()
    {
        begin();
        $ids = request()->post('ids');;
        // Category::whereIn("id", $ids)->delete();
        foreach (Role::whereIn("id", $ids)->get() as  $k => $v) {
            //盘点能否删除
            $v->delete();
        }
        commit();
        return success("删除成功！");
    }
    public function browser()
    {
        if (post("?")) {
            return Role::search(post("search"))
                ->addSelect("sys_role.id as role_id")
                ->addSelect("sys_role.name as role.name")

                ->getGrid();
        } else {
            return view('BaseData.RoleController.browser', F());
        }
    }

    public function access()
    {
        $id = get("id");
        if (post("?")) {
            return RoleBasedAccessControl::relatedJoin()->search(post("search"))->getGrid();
        } else {
            return view('BaseData.RoleController.access', F());
        }
    }


    public function webaccess()
    {
        $id = get("id");
        if (post("?")) {
            return (Menu::relatedJoin()->orderBy("sort")->treeGrid());
        } else {
            return view('BaseData.RoleController.webaccess', F());
        }
    }

    public function enablewebaccess()
    {
        if (post("?")) {
            $menu_ids = post("menu_ids");
            $role_id = post("role_id");
            if (Role::where(['id' => $role_id])->exists())
                try {
                    foreach ($menu_ids as $id) {
                        if (Menu::where(['id' => $id])->exists()) {
                            RoleMenu::firstOrCreate(['role_id' => $role_id, 'menu_id' => $id]);
                        }
                    }
                    return success("授权成功！");
                } catch (\Exception $e) {
                    rollback();
                    return error($e->getMessage());
                }
        }
        return error("授权失败！");
    }
    public function disablewebaccess()
    {
        if (post("?")) {
            $menu_ids = post("menu_ids");
            $role_id = post("role_id");
            try {
                Menu::whereIn("menu_id", $menu_ids)->where(['role_id' => $role_id])->delete();
                return success("取消授权成功！");
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }
        }
        return error("取消授权失败！");
    }
    public function enableaccess()
    {
        if (post("?")) {
            $ControlIds = post("ControlIds");
            $role_id = post("role_id");
            if (Role::where(['id' => $role_id])->exists())
                try {
                    foreach ($ControlIds as $id) {
                        if (RoleBasedAccessControl::where(['id' => $id])->exists()) {
                            RolePermission::firstOrCreate(['role_id' => $role_id, 'controller_id' => $id]);
                        }
                    }
                    return success("授权成功！");
                } catch (\Exception $e) {
                    rollback();
                    return error($e->getMessage());
                }
        }
        return error("授权失败！");
    }

    public function disableaccess()
    {
        if (post("?")) {
            $ControlIds = post("ControlIds");
            $role_id = post("role_id");
            try {
                RolePermission::whereIn("controller_id", $ControlIds)->where(['role_id' => $role_id])->delete();
                return success("取消授权成功！");
            } catch (\Exception $e) {
                rollback();
                return error($e->getMessage());
            }
        }
        return error("取消授权失败！");
    }
}
